package com.hhm.controller;

import com.github.pagehelper.PageInfo;
import com.hhm.ann.MyLog;
import com.hhm.pojo.*;
import com.hhm.service.MainsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 控制层
 */
@RequestMapping("mains")
@Controller
public class MainsController {
    @Autowired
    private MainsService ms;

    /**
     * 获取数据库统计数据
     * @return
     */
    @ResponseBody
    @RequestMapping("getReport")
    public List<Mains> getReport(){
        return ms.count();
    }

    /**
     * 报表
     * @return
     */
    @RequestMapping("toReport")
    public String toReport(){
        return "report";
    }

    /**
     * 批删
     * @param ids
     * @return
     */
    @ResponseBody
    @RequestMapping("dels")
    public boolean dels(@RequestParam("ids[]") Integer[] ids){
        int i = ms.del(ids);

        return true;
    }

    /**
     * 添加 + 标记注解
     * @param mains
     * @return
     */
    @MyLog(value = "增加")
    @ResponseBody
    @RequestMapping("add")
    public Map<String,String> add(Mains mains){//添加的对象
        HashMap<String, String> map = new HashMap<>();
        //没有数据
        List<Infos> infos = mains.getInfos();
        map.put("error", "");
        if (infos==null || infos.size()==0){
            map.put("error", "核酸采集点至少录入一条");
            return map;
        }
        //有行数据，但是存在空值
        for (Infos info : infos) {
            if (StringUtils.isEmpty(info.getStartTime()) || StringUtils.isEmpty(info.getEndTime()) || StringUtils.isEmpty(info.getPrice())){
                map.put("error", "核酸采集点不能有空值");
                return map;
            }
        }
        //没有错误信息--执行添加
        ms.add(mains);
        map.put("success", "保存成功");
        return map;
    }
    /*@ResponseBody
    @RequestMapping("add")
    public boolean add(Mains mains){
        ms.add(mains);
        return true;
    }*/

    /**
     * 去添加
     * @param model
     * @return
     */
    @RequestMapping("toAdd")
    public String toAdd(Model model){
        model.addAttribute("mains", new Mains());
        return "add";
    }

    /**
     * 地区查询--ajax
     * @return
     */
    @ResponseBody
    @RequestMapping("area")
    public List<Area> area(){
        return ms.areas();
    }
    /**
     * 乡镇查询--ajax
     * @return
     */
    @ResponseBody
    @RequestMapping("country")
    public List<Country> country(){
        return ms.countrys();
    }
    /**
     * 小区查询--ajax
     * @return
     */
    @ResponseBody
    @RequestMapping("garden")
    public List<Garden> garden(){
        return ms.gardens();
    }

    /**
     * 列表查询
     * @param mains
     * @param pageNum
     * @param pageSize
     * @param model
     * @return
     */
    @RequestMapping("list")
    public String list(Mains mains, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "3") Integer pageSize, Model model){
        //获取查询数据
        PageInfo<Mains> pageInfo = ms.findAllByPage(mains, pageNum, pageSize);

        //将数据存入作用域
        model.addAttribute("pg", pageInfo);
        model.addAttribute("mains", mains);

        return "list";
    }
}
